import random
from threading import Timer
import sys
from common.utility import Utility

# 主函数
def main(thread_count: int = 500):
    Utility.print_info('entered.')
    for i in range(thread_count):
        create_timer(i)
    Utility.print_info('exited.')

# 创建定时器
def create_timer(idx: int) -> None:
    interval: float = random.random()
    timer: Timer = Timer(interval, task, (idx,))
    timer.start()
    if random.random() < 0.5:
        timer.cancel()
        Utility.print_info(f'timer {idx} is created, then canceled.')
    else:
        Utility.print_info(f'timer {idx} will elapse after {interval} seconds.')

# 任务
def task(idx: int) -> None:
    Utility.print_info(f'timer {idx} elapsed.')

print(f'is gil enabled: {sys._is_gil_enabled()}')
print(f'python version: {sys.version}')
main(5)
